﻿using InterLock.Algorithm;
using InterLock.Helper;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace InterLock
{
    public partial class frmMain : Form
    {
        private event Action commandChangdeEvent;

        private StationMap stationMap = new StationMap();

        private int command = 1;      //按钮命令

        public int Command
        {
            get
            {
                return command;
            }
            set
            {
                command = value;
                //通知命令已更改
                commandChangdeEvent();
            }
        }

        public frmMain()
        {
            InitializeComponent();
            WindowState = FormWindowState.Maximized;
            commandChangdeEvent += FrmMain_commandChangdeEventHandler;
        }

        /// <summary>
        /// 当命令更改时
        /// </summary>
        private void FrmMain_commandChangdeEventHandler()
        {
            btnZQX.BackColor = Color.White;
            btnZDW.BackColor = Color.White;
            btnZFW.BackColor = Color.White;
            btnZY.BackColor = Color.White;
            btnKX.BackColor = Color.White;
        }

        private void btnZQX_Click(object sender, System.EventArgs e)
        {
            if (Command != 2)
            {
                MessageBox.Show("现为总取消按钮！", "提示！");
                Command = 2;
                btnZQX.BackColor = Color.Yellow;
            }
            else
            {
                Command = 1;
                btnZQX.BackColor = Color.White;
            }
        }

        private void btnZDW_Click(object sender, System.EventArgs e)
        {
            if (Command != 5)
            {
                MessageBox.Show("现为总定位按钮！", "提示！");
                Command = 5;
                btnZDW.BackColor = Color.Yellow;
            }
            else
            {
                Command = 1;
                btnZDW.BackColor = Color.White;
            }
        }

        private void btnZFWb_Click(object sender, System.EventArgs e)
        {
            if (Command != 6)
            {
                MessageBox.Show("现为总反位按钮！", "提示！");
                Command = 6;
                btnZFW.BackColor = Color.Yellow;
            }
            else
            {
                Command = 1;
                btnZFW.BackColor = Color.White;
            }
        }

        private void btnZY_Click(object sender, System.EventArgs e)
        {
            if (Command != 3)
            {
                MessageBox.Show("现为区段占用按钮！", "提示！");
                Command = 3;
                btnZY.BackColor = Color.Yellow;
            }
            else
            {
                Command = 1;
                btnZY.BackColor = Color.White;
            }
        }

        private void btnKX_Click(object sender, System.EventArgs e)
        {
            if (Command != 4)
            {
                MessageBox.Show("现为区段空闲按钮！", "提示！");
                Command = 4;
                btnKX.BackColor = Color.Yellow;
            }
            else
            {
                Command = 1;
                btnKX.BackColor = Color.White;
            }
        }


        private void pbMap_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.HighQuality;               //图片柔顺模式选择
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
            g.CompositingQuality = CompositingQuality.HighQuality;     //再加一点


            stationMap.DrawMap(g);  //绘制站场图
        }

        /// <summary>
        /// 按钮按下
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMap_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                stationMap.ButtonDown(e.Location, ref command);

                if (stationMap.IsDonw)
                {
                    pbMap.Refresh();
                    stationMap.IsDonw = false;
                }
            }
        }

        private void tsmZQX_Click(object sender, EventArgs e)
        {
            this.btnZQX_Click(null, null);
        }

        private void tsmZDW_Click(object sender, EventArgs e)
        {
            this.btnZDW_Click(null, null);
        }

        private void tsmZFW_Click(object sender, EventArgs e)
        {
            this.btnZFWb_Click(null, null);
        }

        private void tsmZY_Click(object sender, EventArgs e)
        {
            this.btnZY_Click(null, null);
        }

        private void tsmKX_Click(object sender, EventArgs e)
        {
            this.btnKX_Click(null, null);
        }
    }
}
